/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 *
 */

/**
 * file description.
 * @file    filename
 * @brief   brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author  author_name
 * @date    last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-08-2               jia fengyi         Create it.
 *
 */

package com.cart.musicdemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.cart.musicdemo.R;
import com.cart.musicdemo.base.service.FileSystemManager;
import com.cart.musicdemo.base.utils.StringUtils;
import com.cart.musicdemo.model.SongModel;

import java.util.List;

/**
 * The type File adapter.
 */
public class FileAdapter extends RecyclerView.Adapter<FileAdapter.ViewHolder> {

    private List<Object> folderNodeAndSongList;

    private FileAdapter.OnItemClickListener listener;

    /**
     * The interface On item click listener.
     */
    public interface OnItemClickListener {
        /**
         * On item click.
         *
         * @param folderNode the folder node
         * @param songModel  the song model
         * @param reset      the reset
         */
        void onItemClick(FileSystemManager.FolderNode folderNode,
                         SongModel songModel,
                         boolean reset);
    }

    /**
     * Instantiates a new File adapter.
     *
     * @param folderNodeAndSongList the folder node and song list
     * @param listener              the listener
     */
    public FileAdapter(List<Object> folderNodeAndSongList, OnItemClickListener listener) {
        this.folderNodeAndSongList = folderNodeAndSongList;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_file_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //对比类属性设置不同image
        if (folderNodeAndSongList.get(position)
                .getClass().equals(FileSystemManager.FolderNode.class)) {
            //文件夹UI逻辑
            FileSystemManager.FolderNode folderNode =
                    (FileSystemManager.FolderNode)
                    folderNodeAndSongList.get(position);
            holder.textView.setText(folderNode.getName());
            holder.imageView.setImageResource(R.drawable.folder);
            //文件监听处理
            holder.relativeLayout.setOnClickListener(
                    v -> listener.onItemClick(folderNode, null, false));
        } else if (folderNodeAndSongList.get(position).getClass().equals(SongModel.class)) {
            //歌曲UI逻辑
            SongModel songModel = (SongModel) folderNodeAndSongList.get(position);
            holder.textView.setText(
                    StringUtils.subString(songModel.getName(), songModel.getSinger()));
            holder.imageView.setTag(R.id.ivSongImage, position);
            //歌曲监听处理
            holder.relativeLayout.setOnClickListener(
                    v -> listener.onItemClick(null, songModel, false));
            if (position == (int) holder.imageView.getTag(R.id.ivSongImage)
                    && ((SongModel) folderNodeAndSongList.get(position)).getPlaying()) {
                holder.imageView.setImageResource(R.drawable.ic_baseline_headset_24);
            } else {
                holder.imageView.setImageResource(R.drawable.ic_baseline_music_note_24);
            }
        }

    }

    @Override
    public int getItemCount() {
        return folderNodeAndSongList.size();
    }

    /**
     * The type View holder.
     */
    public class ViewHolder extends RecyclerView.ViewHolder {
        /**
         * The Text view.
         */
        TextView textView;
        /**
         * The Image view.
         */
        ImageView imageView;

        RelativeLayout relativeLayout;

        /**
         * Instantiates a new View holder.
         *
         * @param itemView the item view
         */
        public ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_fileName);
            imageView = itemView.findViewById(R.id.tv_iamge);
            relativeLayout = itemView.findViewById(R.id.rl_file_click);
        }
    }
}
